package com.xxmw.transaction.controller.app;

import com.xxmw.transaction.common.annotation.LoginUser;
import com.xxmw.transaction.common.entity.R;
import com.xxmw.transaction.common.utils.Assert;
import com.xxmw.transaction.common.utils.bourse.HuobiUtil;
import com.xxmw.transaction.enumeration.BourseEnum;
import com.xxmw.transaction.model.app.BourseApi;
import com.xxmw.transaction.model.app.User;
import com.xxmw.transaction.service.app.BourseApiService;
import com.xxmw.transaction.service.app.SymbolSubService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;
import java.util.List;

/**
 * 交易所api管理
 * @since  2020/2/17 10:36
 * @author luowenliang
 */
@Slf4j
@RestController
@RequestMapping("/app/bourse/api")
@Api(tags = "交易所api管理")
public class BourseApiController {

	@Autowired
	private BourseApiService bourseApiService;

	@Autowired
	private SymbolSubService symbolSubService;


	/**
	 * 获取用户api列表
	 * @param user 用户信息
	 * @return
	 */

	@GetMapping("/list")
	@ApiOperation("获取用户api列表")
	public R list(@ApiIgnore @LoginUser User user) {
		List<BourseApi> userApis = bourseApiService.getUserApis(user.getUsername());
		return R.ok(userApis);
	}

	/**
	 * 信息获取
	 * @param bourse api的id
	 * @return
	 */
	@GetMapping("/info")
	@ApiOperation("获取单个api的详情")
	public R info(@RequestParam(value = "bourse", required = false) Integer bourse) {
		BourseApi api = bourseApiService.getById(bourse);
		return R.ok(api);
	}

	/**
	 * 新增或保存
	 * @param bourseApi
	 * @return
	 */
	@PostMapping("/saveOrUpdate")
	@ApiOperation("新增/修改api")
	public R saveOrUpdate(@LoginUser User user, BourseApi bourseApi) {
//
//		BourseApi old = bourseApiService.getUserApi(user.getUsername(), bourseApi.getBourse());

//		if (null != old && !old.getId().equals(bourseApi.getId())) {
//			return R.error("您已添加过该交易所API");
//		}

		if (null == bourseApi.getAccessKey() || null == bourseApi.getSecretKey()) {
			return R.error("access key和secret key为必填参数");
		}

		// OK 需要passphrase
		if (BourseEnum.OKEX.getValue() == bourseApi.getBourse() && null == bourseApi.getPassphrase()) {
			return R.error("请填写账号秘钥");
		}

		if (null == bourseApi.getId() || 0 == bourseApi.getId()) {
			bourseApi.setCreated(new Date());
			bourseApi.setUsername(user.getUsername());
		}

		//获取用户的现货账户ID只有火币需要
		if (BourseEnum.HUOBI.getValue() == bourseApi.getBourse()) {
			bourseApi.setAccountId(HuobiUtil.accountId(bourseApi.getAccessKey(), bourseApi.getSecretKey()));
		} else {
			bourseApi.setAccountId(0L);
		}
		bourseApi.setUpdated(new Date());

		// 测试是否可用
		try {
			symbolSubService.getUSDTAmount(bourseApi);
		} catch (Exception e) {
			return R.error("该api无法使用或交易所繁忙");
		}
		bourseApiService.saveOrUpdate(bourseApi);
		return R.ok();
	}


	/**
	 * 测试api是否可以使用
	 *
	 * @return
	 */
	@ApiOperation("测试api是否可以使用")
	@GetMapping("testApi")
	public R testApi(@RequestParam Long id) {
		BourseApi api = bourseApiService.getById(id);
		if (Assert.isNullOrEmpty(api)) {
			return R.ok("api不存在");
		}
		try {
			symbolSubService.getUSDTAmount(id);
		} catch (Exception e) {
			return R.error("该api无法使用或交易所繁忙");
		}
		return R.ok();
	}
}
